package cn.edu.ldu.views.operator;


import cn.edu.ldu.controller.UserController;
import cn.edu.ldu.entity.FriendsEntity;
import cn.edu.ldu.protocol.Protocol;
import cn.edu.ldu.util.Consts;
import cn.edu.ldu.util.L;
import cn.edu.ldu.util.Translate;
import cn.edu.ldu.views.login.LoginUI;
import com.alibaba.fastjson.JSON;

import javax.swing.*;
import java.io.IOException;
import java.net.*;

/**
 *
 * @author DYZ
 */
public class AddFriends extends javax.swing.JFrame {

    /**
     * Creates new form AddFriends
     */
    public AddFriends(FriendsEntity friendsEntity) {
        friend = friendsEntity;
        initComponents(friendsEntity);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents(FriendsEntity friendsEntity) {

        jPanel1 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtUserId = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtGender = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        btnAdd.setText("确定添加");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(149, 149, 149)
                                .addComponent(btnAdd)
                                .addContainerGap(212, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addContainerGap(38, Short.MAX_VALUE)
                                .addComponent(btnAdd)
                                .addGap(35, 35, 35))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("好友信息"));

        jLabel1.setText("账户：");

        txtUserId.setText(friendsEntity.getUserAccountEntity().getUserId().toString());
        txtUserId.setEnabled(false);

        jLabel2.setText("昵称：");

        txtUsername.setText(friendsEntity.getUserAccountEntity().getRealName());
        txtUsername.setEnabled(false);

        jLabel3.setText("性别：");

        if (friendsEntity.getUserAccountEntity().getGender()==null){
            txtGender.setText("");
        }else {
            txtGender.setText(friendsEntity.getUserAccountEntity().getGender().toString());
        }
        txtGender.setEnabled(false);

        jLabel4.setText("个性签名：");

        if (friendsEntity.getUserAccountEntity().getSignature() == null){
            jTextArea1.setText("");
        }else {
            jTextArea1.setText(friendsEntity.getUserAccountEntity().getSignature());
        }
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setEnabled(false);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel1)
                                                .addComponent(jLabel2)
                                                .addComponent(jLabel3))
                                        .addComponent(jLabel4))
                                .addGap(35, 35, 35)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(txtUserId)
                                                .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                                                .addComponent(txtGender))
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(29, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3)
                                        .addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(jLabel4)
                                                .addGap(0, 90, Short.MAX_VALUE))
                                        .addComponent(jScrollPane1))
                                .addContainerGap())
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>

    /**
     * 添加按钮
     * 1、询问是否添加
     * @param evt
     */
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt){
        int res = JOptionPane.showConfirmDialog(null, "是否添加为好友", "是否继续", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {
            // 点击“是”后执行这个代码块
            //提交
            //提交给服务器
            try {
                remoteAddr = InetAddress.getByName(Consts.ipv4);
                DatagramSocket clientSocket = new DatagramSocket();
                clientSocket.setSoTimeout(5000);

                Protocol protocol = new Protocol();
                protocol.setKey("AddFriend");
                protocol.setToken(UserController.getToken());
                FriendsEntity friendsEntity = new FriendsEntity();
                friendsEntity.setUserId(UserController.getUserEntity().getId());
                friendsEntity.setFriendId(friend.getUserAccountEntity().getUserId());

                protocol.setData(JSON.toJSONString(friendsEntity));
                byte[] data = Translate.ObjectToByte(protocol);

                DatagramPacket packet = new DatagramPacket(data,data.length,remoteAddr,Consts.port);
                clientSocket.send(packet);
                L.OK(packet,AddFriends.class);

                Protocol pro = (Protocol) Translate.ByteToObject(packet.getData());
                pro.key();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //提交失败
        } else {
            return;
        }
    }

    private void formWindowClosed(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:
        this.dispose();
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnAdd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField txtGender;
    private javax.swing.JTextField txtUserId;
    private javax.swing.JTextField txtUsername;
    InetAddress remoteAddr = null;
    private FriendsEntity friend;
    // End of variables declaration
}
